package com.kgc.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.novel.common.Res;
import com.kgc.novel.dao.BookContentMapper;
import com.kgc.novel.domain.entity.BookContent;
import com.kgc.novel.domain.param.BookContentParam;
import com.kgc.novel.domain.vo.BookContentVO;
import com.kgc.novel.service.book.BookContentService;
import com.kgc.novel.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class BookContentServiceImpl extends ServiceImpl<BookContentMapper, BookContent> implements BookContentService{

    @Resource
    private BookContentMapper bookContentMapper;

    @Override
    public Res selectContentList(BookContentParam bookContentParam) {
        Objects.isNull(bookContentParam);
        List<BookContentVO> allList = bookContentMapper.getAllList(bookContentParam);
        long count = bookContentMapper.getCount(bookContentParam.getBookId(),
                bookContentParam.getChapter(),
                bookContentParam.getStatus());
        return Res.returnData(allList,count);
    }

    @Override
    public Res selectContent(String id) {
        LambdaQueryWrapper<BookContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookContent::getId,id);

        BookContent bookContent1 = bookContentMapper.selectOne(lambdaQueryWrapper);
        if (bookContent1 != null){
            return Res.returnData(bookContent1);
        }
        return Res.returnMsg("暂无该内容信息！");
    }

    @Override
    public Res addContent(BookContent bookContent) {
        LambdaQueryWrapper<BookContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookContent::getChapter,bookContent.getChapter())
                .eq(BookContent::getBookId,bookContent.getBookId());

        BookContent bookContent1 = bookContentMapper.selectOne(lambdaQueryWrapper);
        if (bookContent1 != null){
            return Res.returnErrMsg("该章节已存在，请重新添加");
        }
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long id = idWorker.nextId();
        bookContent.setId(String.valueOf(id));
        bookContentMapper.insert(bookContent);
        return Res.returnMsg("添加成功！");
    }

    @Override
    public Res updateContent(BookContent bookContent) {
        LambdaQueryWrapper<BookContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookContent::getChapter,bookContent.getChapter())
                .eq(BookContent::getBookId,bookContent.getBookId());

        BookContent bookContent1 = bookContentMapper.selectOne(lambdaQueryWrapper);
        if (bookContent1 == null){
            bookContent.setUpdateTime(LocalDateTime.now());
            bookContentMapper.updateById(bookContent);
            return Res.returnMsg("修改成功！");
        }

        BookContentVO single = bookContentMapper.getSingle(bookContent.getId());
        if (single.getChapter().equals(bookContent.getChapter()) && single.getBookId().equals(bookContent.getBookId())){
            bookContent.setUpdateTime(LocalDateTime.now());
            bookContentMapper.updateById(bookContent);
            return Res.returnMsg("修改成功！");
        }
        return Res.returnErrMsg("该章节已存在，请重新修改！");
    }

    @Override
    public Res deleteContent(String id) {
        int i = bookContentMapper.deleteSingle(id);
        if (i != -1){
            return Res.returnMsg("删除成功！");
        }else {
            return Res.returnErrMsg("删除失败！");
        }
    }

    @Override
    public Res deleteAllContent(List<String> ids) {
        int i = bookContentMapper.deleteAllSingle(ids);
        if (i != -1){
            return Res.returnMsg("删除成功！");
        }else {
            return Res.returnErrMsg("删除失败！");
        }
    }

}
